package org.hijava.iplooking;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IPQuery extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String ipStr=req.getParameter("ip");
		Long ipValue=Utils.getIpNumber(ipStr);
		PersistenceManager pm=PMF.get().getPersistenceManager();
		Query query=pm.newQuery(IP.class,"startIP<=ip && endIP>=ip");
		query.declareParameters("long ip");
		List<IP> result=(List<IP>) query.execute(ipValue);
		req.setAttribute("result", result.get(0));
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
